﻿#pragma once
#include "HashTable.h"
namespace bit
{

	template<class K, class V, class Hash = HashFunc<K>>
	class unordered_map
	{
		struct MapKeyOfT
		{
			const K& operator()(const pair<K,V>& kv)
			{
				return kv.first;
			}
		};
	public:
		typedef typename hash_bucket::HashTable<K, pair<const K, V>, MapKeyOfT, Hash>::Iterator iterator;
		typedef typename hash_bucket::HashTable<K, pair<const K, V>, MapKeyOfT, Hash>::ConstIterator const_iterator;

		iterator begin()
		{
			return _ht.Begin();
		}
		iterator end()
		{
			return _ht.End();
		}
		const_iterator begin() const
		{
			return _ht.Begin();
		}
		const_iterator end() const
		{
			return _ht.End();
		}
		pair<iterator, bool> insert(const pair<K, V>& key)
		{
			return _ht.Insert(key);
		}
		iterator find(const K& key)
		{
			return _ht.Find(key);
		}
		bool erase(const K& key)
		{
			return _ht.Erase(key);
		}
		V& operator[](const K& key)
		{
			pair<iterator, bool> ret = insert({ key, V() });
			return ret.first->second;
		}
	private:
		hash_bucket::HashTable<K, pair<const K, V>, MapKeyOfT, Hash> _ht;
	};
	void test_map()
	{
		unordered_map<string, string> m;
		m.insert({ "sort", "排序" });
		m.insert({ "left", "左边" });
		m.insert({ "right", "右边" });
		// 插入
		m["insert"];
		for (auto& e : m)
		{
			// e.first += 'x';
			//e.second += 'x';

			cout << e.first << ":" << e.second << endl;
		}
		cout << endl;

		// 修改
		m["insert"] = "插入";
		for (auto& e : m)
		{
			// e.first += 'x';
			//e.second += 'x';

			cout << e.first << ":" << e.second << endl;
		}
		cout << endl;

		// 插入+修改
		m["string"] = "字符串";
		for (auto& e : m)
		{
			// e.first += 'x';
			//e.second += 'x';

			cout << e.first << ":" << e.second << endl;
		}
		cout << endl;


		//for (auto& e : m)
		//{
		//	// e.first += 'x';
		//	//e.second += 'x';

		//	cout << e.first << ":" << e.second << endl;
		//}

		//Print(m);

		string arr[] = { "苹果", "西瓜", "苹果", "西瓜", "苹果", "苹果", "西瓜","苹果", "香蕉", "苹果", "香蕉" };
		unordered_map<string, int> countMap;
		for (auto& str : arr)
		{
			countMap[str]++;
		}

		for (auto& e : countMap)
		{
			cout << e.first << ":" << e.second << endl;
		}
		cout << endl;
	}
}